package com.android.namecard.activity;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.namecard.R;

public class SendMessageActivity extends Activity{
	
	EditText receiver;
    EditText content;
    Button send;
    SmsManager smsManager;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.send_message);
        
        //获取传入的手机参数
        Bundle bundle = this.getIntent().getExtras();
        String phone = bundle.getString("phone");
        
        //获取界面上的变量
        receiver = (EditText)findViewById(R.id.message_receiver);
        content = (EditText)findViewById(R.id.message_content);
        send = (Button)findViewById(R.id.send_message);
        receiver.setText(phone);
        
        /*建构一取得default instance的 SmsManager对象 */ 
        smsManager = SmsManager.getDefault(); 
        //按钮事件响应
        send.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				 try { 
			         /*两个条件都检查通过的情况下,发送简讯 * 
			          * 先建构一PendingIntent对象并使用getBroadcast()方法进行Broadcast * 
			          * 将PendingIntent,电话,简讯文字等参数传入sendTextMessage()方法发送简讯*/ 
			         PendingIntent mPI = PendingIntent.getBroadcast(SendMessageActivity.this, 0, new Intent(), 0); 
			         smsManager.sendTextMessage(receiver.getText().toString(), null, content.getText().toString(), mPI, null);
			     }catch(Exception e){
			         e.printStackTrace();
			     }
			     Toast.makeText(SendMessageActivity.this, "送出成功!!" , Toast.LENGTH_SHORT).show();
			     SendMessageActivity.this.finish();
			}
        });
    }
}
